<?php

//关于endpoint的介绍见, endpoint就是OSS访问的域名
//http://docs.aliyun.com/?spm=5176.7114037.1996646101.11.XMMlZa&pos=6#/oss/product-documentation/domain-region
class AliyunOss
{
    const endpoint = OSS_ENDPOINT;
//    const accessKeyId = OSS_ACCESS_ID;
//    const accessKeySecret = OSS_ACCESS_KEY;
    const bucket = OSS_TEST_BUCKET;

    private $accessKeyId = OSS_ACCESS_ID;
    private $accessKeySecret = OSS_ACCESS_KEY;
    private $securityToken = null;

    private $oss;

    public function __construct()
    {
        $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'oss' . DIRECTORY_SEPARATOR;
        require_once $path . 'sdk.class.php';
        require_once $path . 'util/oss_util.class.php';

        if (OSS_IS_STS)
        {
            $this->get_access_token();
        }
        $this->oss = new ALIOSS($this->accessKeyId, $this->accessKeySecret, self::endpoint, $this->securityToken);
        $this->oss->set_enable_domain_style(true);
        $this->create_bucket();
    }

    public function get_oss_client()
    {
        return $this->oss;
    }

    public function get_bucket_name()
    {
        return self::bucket;
    }

    public function create_bucket()
    {
        $acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
        $this->oss->create_bucket(self::bucket, $acl);
    }

    public function get_object_url($object, $prefix = '')
    {
        return 'https://' . self::bucket . '.' . self::endpoint . (empty($prefix) ? '' : '/' . $prefix) . '/' . $object;
    }

    private function get_access_token()
    {
        if (!$this->is_credentials_expired())
        {
            $content = file_get_contents(OSS_ACCESS_URL);
            $result = json_decode($content, true);
            $_SESSION['OSS_CREDENTIALS'] = $result['result']['Credentials'];
        }
        $this->accessKeyId = $_SESSION['OSS_CREDENTIALS']['AccessKeyId'];
        $this->accessKeySecret = $_SESSION['OSS_CREDENTIALS']['AccessKeySecret'];
        $this->securityToken = $_SESSION['OSS_CREDENTIALS']['SecurityToken'];
    }

    private function is_credentials_expired()
    {
        return isset($_SESSION['OSS_CREDENTIALS']['Expiration']) && strtotime($_SESSION['OSS_CREDENTIALS']['Expiration']) >= time();
    }

    function upload_file_by_file($file_path, $file_name, $options = null)
    {
        try
        {
            $this->oss->upload_file_by_file(self::bucket, $file_name, $file_path, $options);
            return 'https://' . self::bucket . '.' . self::endpoint . '/' . $file_name;
        }
        catch (Exception $ex)
        {
            return false;
        }
    }

    function batch_upload_file($object, $directory)
    {
        $options = array(
            'bucket' => self::bucket,
            'object' => $object,
            'directory' => $directory,
        );
        try
        {
            return $this->oss->batch_upload_file($options);
        }
        catch (Exception $ex)
        {
            return false;
        }
    }

    function delete_file($object)
    {
        $object = str_replace('https://' . self::bucket . '.' . self::endpoint . '/', '', $object);
        $this->oss->delete_object(self::bucket, $object);
    }
}